package http

import (
	"gitee.com/simple-set/simple.io/src/event"
	"gitee.com/simple-set/simple.io/src/process/codec"
)

type RequestProcess struct {
	decoder *RequestDecoder
}

func (r *RequestProcess) Accept(session *event.Session) error {
	return nil
}

func (r *RequestProcess) Disconnect(session *event.Session) error {
	return nil
}

func (r *RequestProcess) Active(session *event.Session, _ any) (any, error) {
	request := NewRequest(codec.NewReadByteBuf(session.Socket.Reader))
	if err := r.decoder.Decoder(request); err != nil {
		return nil, err
	}

	response := NewReplyResponse(request)
	response.BufWriter = codec.NewWriteByteBuf(session.Socket.Writer)
	session.SetAttr("response", response)
	session.SetAttr("request", request)
	return request, nil
}

func NewRequestProcess() *RequestProcess {
	return &RequestProcess{decoder: NewRequestDecoder()}
}
